package com.wewoo.log4e.core.spring.boot.autoconfigure;

import com.wewoo.log4e.core.LogTemplate;
import com.wewoo.log4e.core.Register.ReadRegister;
import com.wewoo.log4e.core.Register.ReadTemplate;
import com.wewoo.log4e.core.aop.LogAspect;
import com.wewoo.log4e.core.context.Log4eHolder;
import com.wewoo.log4e.core.executor.LogExecutor;
import com.wewoo.log4e.core.util.ReadLogUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @ClassName LogAutoConfiguration
 * @Description 日志自动配置器
 * @Author tangjin
 * @Date 16:48 2022/2/14
 **/
@RequiredArgsConstructor
@Configuration
@EnableConfigurationProperties(Log4eProperties.class)
public class LogAutoConfiguration {
    private final Log4eProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public LogTemplate logTemplate(List<LogExecutor> logExecutors) {
        LogTemplate logTemplate = new LogTemplate();
        logTemplate.setExecutors(logExecutors);
        logTemplate.setLog4eProperties(properties);
        return logTemplate;
    }

    @Bean
    @ConditionalOnMissingBean
    public ReadRegister readRegister(List<ReadLogUtil> readLogUtils) {
        return new ReadRegister(readLogUtils);
    }

    @Bean
    @ConditionalOnMissingBean
    public ReadTemplate readTemplate(ReadRegister readRegister,LogTemplate logTemplate){
        return new ReadTemplate(readRegister,logTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public LogAspect logAspect(Log4eProperties properties, LogTemplate logTemplate, Log4eHolder log4eHolder) {
        return new LogAspect(logTemplate, properties, log4eHolder);
    }
}
